package org.ehe.erp.controller.purchase;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.oss.entity.UploadResult;
import org.ehe.erp.domain.purchase.vo.ErpPurchaseOrderAttachmentVo;
import org.ehe.erp.service.purchase.IErpPurchaseOrderAttachmentService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 订单附件信息
 * @author :zhangnn
 * @className :ErpPurchaseOrderAttachmentController
 * @description: TODO
 * @date 2025-09-23 09:43:39
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/purchaseOrderAttachment")
public class ErpPurchaseOrderAttachmentController extends BaseController {

    private final IErpPurchaseOrderAttachmentService attachmentService;

    /**
     * 通用上传（返回路径）
     */
    @SaCheckPermission("erp:purchaseOrderAttachment:add")
    @PostMapping("/upload")
    public R<UploadResult> upload(@RequestParam("file") MultipartFile file) {
        return R.ok(attachmentService.upload(file));
    }

    /**
     * 根据采购ID查询附件列表
     */
    @SaCheckPermission("erp:purchaseOrderAttachment:query")
    @GetMapping("/listByPurchaseOrderId/{orderId}")
    public R<List<ErpPurchaseOrderAttachmentVo>> listByPurchaseOrderId(@PathVariable Long orderId) {
        return R.ok(attachmentService.listById(orderId));
    }

    /**
     * 删除附件（物理删）
     */
    @SaCheckPermission("erp:purchaseOrderAttachment:remove")
    @Log(title = "采购附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(attachmentService.deleteWithValidByIds(List.of(ids)));
    }
}
